import { DateTime } from 'luxon'
import { belongsTo, column } from '@adonisjs/lucid/orm'
import type { BelongsTo } from '@adonisjs/lucid/types/relations'
import CommonBaseModel from '#common/base_model'
import StockInDetail from '#models/stock_in_detail'
import StockOutDetail from '#models/stock_out_detail'

export default class StockOrder extends CommonBaseModel {
  /** 库存方式ID */
  @column()
  declare fkStockMethodId: number

  /** 仓库ID */
  @column()
  declare fkWarehouseId: number

  /** 会员ID */
  @column()
  declare fkMemberId: number

  /** 销售单号 */
  @column()
  declare billNo: string

  /** 销售日期 */
  @column({
    serialize(value) {
      return DateTime.fromFormat(value, 'yyyy-MM-dd')
    },
  })
  declare billDate: Date

  /** 销售类型 */
  @column()
  declare billType: string

  /** 库存类型 */
  @column()
  declare stockType: string

  /** 库存方式 */
  @column()
  declare stockWay: string

  /** 入库单号 */
  @column()
  declare inOrderNo: string

  /** 出库单号 */
  @column()
  declare outOrderNo: string

  /** 总数量 */
  @column()
  declare totalQuantity: number

  /** 总价格 */
  @column()
  declare totalAmount: number

  /** 状态 */
  @column()
  declare status: number

  /** 备注 */
  @column()
  declare memo: string

  @belongsTo(() => StockInDetail)
  declare stockInDetail: BelongsTo<typeof StockInDetail>

  @belongsTo(() => StockOutDetail)
  declare stockOutDetail: BelongsTo<typeof StockOutDetail>
}
